package model;

import java.io.Serializable;

public class Position implements Serializable {
	private static final long serialVersionUID = 1L;
	private float x;// first member of pair
	private float y;// second member of pair
	private float movementScale = 10;

	public Position(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public Position(String s) {
		String delims = ";";
		s = s.replace("(", "").replace(")", "");
		String[] tokens = s.split(delims);

		if (tokens.length == 2) {
			x = Float.parseFloat(tokens[0]);
			y = Float.parseFloat(tokens[1]);
		}
	}

	public void setFirst(float x) {
		this.x = x;
	}

	public void setSecond(float y) {
		this.y = y;
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	@Override
	public String toString() {
		return "(" + x + ";" + y + ")";
	}
	
//	@Override
//	public String toString() {
//		return "(" + String.format("%.3g%n", x) + ";" +  String.format("%.3g%n",y) + ")";
//	}

	public Position goRight() {
		return new Position(x + 1/movementScale, y);
	}

	public Position goLeft() {
		return new Position(x - 1/movementScale, y);
	}

	public Position goUp() {
		return new Position(x, y + 1/movementScale);
	}

	public Position goDown() {
		return new Position(x, y - 1/movementScale);
	}
	
	public Position goVector(Position p){
		return new Position(x+p.x,y+p.y);
	}

	public boolean inBounds() {
		if (x > 100 || x < 0 || y > 100 || y < 0)
			return false;
		else
			return true;
	}
	
	public float findDistance(Position other)
	{
		
		 float distance = (float) Math.sqrt((other.x-x)*(other.x-x) + (other.y-y)*(other.y-y));
		 return distance;
	}
	
	public Position getMovementVector(Position other){
		float d = findDistance(other);
		if(d > 1)
			return new Position((other.x-x)/(d*movementScale),(other.y-y)/(d*movementScale));
		else
			return new Position((other.x-x)/movementScale,(other.y-y)/movementScale);
	}

	public float getScalar(){
		return (float) Math.sqrt(x*x+y*y);
	}
	
}
